package com.jt.service;

import com.jt.mapper.ItemCatMapper;
import com.jt.pojo.ItemCat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
public class ItemCatServiceimpl implements ItemCatService {

    @Autowired
    private ItemCatMapper itemCatMapper;

    /*业务：Map<parentId>,List<ItemCat>
    * 思路:判断Map中是否有key
    * true:  获取list集合,将自己追加到集合中
    * false:new list集合,将自己作为第一个元素保存,之后存储map */

    public Map<Integer,List<ItemCat>> getMap(){
        Map<Integer,List<ItemCat>> map= new HashMap<>();
        //1,查询所有数据库信息
        List<ItemCat> list= itemCatMapper.findItemCatList();
        //2,遍历数据封装Map
        for(ItemCat itemCat : list){
            int key = itemCat.getParentId();
            if (map.containsKey(key)){
                map.get(key).add(itemCat);
            }else{//key不存在,第一个数据
                List<ItemCat> temList=new ArrayList<>();
                temList.add(itemCat);
                map.put(key,temList);
            }
        }
        return map;
    }

    @Override
    public List<ItemCat> findItemCatList(Integer level) {
        Map<Integer,List<ItemCat>> map = getMap();
        if (level == 1){
            return map.get(0);
        }
        if (level == 2)

        return getTwolist(map);
        return getThreeList(map);
    }

    @Override
    public void saveItemCat(ItemCat itemCat) {
        Date date = new Date();
        itemCat.setStatus(true)
                .setCreated(date)
                .setUpdated(date);
        itemCatMapper. saveItemCat(itemCat);
    }

    @Override
    @Transactional
    public void updateItemCat(ItemCat itemCat) {
        //只需要修改name/updated
        itemCat.setUpdated(new Date());
        itemCatMapper.updateItemCat(itemCat);
    }

    @Override
    public void deleteItemCat(ItemCat itemCat) {
        //判断level到底是几级菜单
        if (itemCat.getLevel() == 3){
            itemCatMapper.deleteItemCatById(itemCat.getId());
        }
        if (itemCat.getLevel() == 2){
            //应该删除三级和二级
            itemCatMapper.deleteItemCat2(itemCat.getId());

        }
        if (itemCat.getLevel() == 1){
            itemCatMapper.deleteItemCat1(itemCat.getId());
        }
    }

    private List<ItemCat> getThreeList(Map<Integer, List<ItemCat>> map) {

        //1,先查询一级和二级的数据
        List<ItemCat> oneList=getTwolist(map);
        //2,遍历一级集合,获取二级数据
        for (ItemCat oneItemCat : oneList){
            //2.1 获取二级数据信息,二级数据可能为null
            List<ItemCat> twoList = oneItemCat.getChildren();
            if(twoList == null || twoList.size()==0)
                continue;
            for (ItemCat twoItemCat : twoList){
                int twoId = twoItemCat.getId();
                //2.2获取三级集合信息
                List<ItemCat> threeList=map.get(twoId);
                //2.3将三级数据封装到二级对象中
                twoItemCat.setChildren(threeList);
            }
        }
        return oneList;
    }

    private List<ItemCat> getTwolist(Map<Integer,List<ItemCat>>Map){
        List<ItemCat> oneList = Map.get(0);
        for (ItemCat oneItemCat : oneList){
            //获取1级Id
            int id =oneItemCat.getId();
            //根据1级id,查询二级数据
            List<ItemCat> twoList= Map.get(id);
            oneItemCat.setChildren(twoList);
        }
        return oneList;
    }


//    @Override
//    public List<ItemCat> findItemCatList(Integer level) {
//        //1,查询一级商品的分类信息
//        int parentId = 0;
//        List<ItemCat> oneList =
//                itemCatMapper.findItemCatByParentId(parentId);
//       //2，查询二级商品的分类信息
//        for (ItemCat oneItemCat : oneList){
//            //2.1根据一级id 查询二级的数据
//            int OneId = oneItemCat.getId();
//            List<ItemCat> twoList = itemCatMapper.findItemCatByParentId(OneId);
//
//            //2，查询三级商品的分类信息
//        for (ItemCat twoItemCat : twoList){
//            int twoId= twoItemCat.getId();
//            List<ItemCat> threeList = itemCatMapper.findItemCatByParentId(twoId);
//       twoItemCat.setChildren(threeList);
//        }
//            oneItemCat.setChildren(twoList);
//        }
//
//        return oneList;
//    }
}
